package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.common.Result;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Employee;
import com.example.service.EmployeeService;
import com.example.service.ExpensesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基础前端接口
 */
@RestController
public class WebController {

    @Resource
    private EmployeeService employeeService;

    @Autowired
    private ExpensesService expensesService;

    @GetMapping("/")
    public Result hello() {
        return Result.success("访问成功");
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody Employee employee) {
        if (ObjectUtil.isEmpty(employee.getUsername()) || ObjectUtil.isEmpty(employee.getPassword() )) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
            employee = employeeService.login(employee);
        return Result.success(employee);
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody Employee employee) {
        if (StrUtil.isBlank(employee.getUsername()) || StrUtil.isBlank(employee.getPassword())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        employeeService.register(employee);
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Employee employee) {
        //进行参数的判断
        if (ObjectUtil.isEmpty(employee.getId()) || StrUtil.isBlank(employee.getPassword())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        //进行密码的修改
        employeeService.updatePassword(employee);
        return Result.success();
    }

    /**
     * 获取到所有的报销类型
     * @return
     */
    @GetMapping("/getExpensesType")
    public Result getExpensesType(){
        List<String> expensesType = expensesService.getExpensesType();
        return Result.success(expensesType);
    }

}
